﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kcadastro.classes;

namespace Kcadastro.os
{
    public partial class osList : System.Web.UI.Page
    {

        private int indice;

        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: BEGIN Seleciona Link Serviços
            if (this.Master is kcadastro)
            {
                ((kcadastro)this.Page.Master).SelecionaPagina(FW.utils.enumPaginas.ListaOS);
            }
            //TODO:END
            masterPageKcad m = Master as masterPageKcad;
            m.idFerr = 4;
            
            if (!seguranca.aplicSegur(4)) Response.Redirect("/na.aspx", true);

            if (!IsPostBack)
            {
                carregar();
            }
        }

        private void carregar()
        {
            oss o = new oss();
            //gv_lista.PageIndex = e.NewPageIndex;
            gv_lista.DataSource = o.listOss(Convert.ToInt16(Session["IDemp"]));
            gv_lista.DataBind();
            o = null;
        }

        protected void gv_lista_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "visualizar")
            {
                int ind = Convert.ToInt32(e.CommandArgument);
                DataKey data = gv_lista.DataKeys[ind];
                Session.Add("IDos", data.Values["PK_IDos"].ToString());
                Response.Redirect("osEdit.aspx", true); //?id=" + data.Values["PK_IDos"].ToString()

            }
            //TODO: BEGIN SJC - verifica o status se pode ser alterado e/ou apagado
            //Data: 10/06/2012
            else if (e.CommandName.ToString() == "apagar")
            {
                tb_o to = new tb_o();
                oss os = new oss();
                indice = Convert.ToInt32(e.CommandArgument);
                int numOs = Convert.ToInt32(gv_lista.Rows[indice].Cells[0].Text);
                to = os.getNumOs(numOs);
                //ImageButton deleteButton = (ImageButton)gv_lista.Rows[indice].Cells[8].Controls[0];
                
                // 99 número criado para alterar o status para apagada. 
                switch (to.stts)
                {
                    case 1:
                        to.stts = 99;
                        to.status = "OS apagada";
                        to.PK_IDos = numOs;
                        to.nomeOS = gv_lista.Rows[indice].Cells[2].Text;
                        to.qtd_registros = Convert.ToInt32(gv_lista.Rows[indice].Cells[4].Text);
                        os.updateOS(to);
                        
                        break;
                    case 2:
                        to.stts = 99;
                        to.status = "OS apagada";
                        to.PK_IDos = Convert.ToInt32(gv_lista.Rows[indice].Cells[0].Text);
                        to.nomeOS = gv_lista.Rows[indice].Cells[2].Text;
                        to.qtd_registros = Convert.ToInt32(gv_lista.Rows[indice].Cells[4].Text);
                        os.updateOS(to);
                        gv_lista.Rows[indice].Enabled = false;
                        break;
                    case 3:
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alerta", "alert('OS não pode ser apagada!')", true);
                        gv_lista.Rows[indice].Cells[7].Enabled = false;
                        break;
                    case 10:
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alerta", "alert('OS não pode ser apagada!')", true);
                        gv_lista.Rows[indice].Cells[7].Enabled = false;
                        break;
                    case 22:
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alerta", "alert('OS não pode ser apagada!')", true);
                        gv_lista.Rows[indice].Cells[7].Enabled = false;
                        break;
                    case 99:
                        gv_lista.Rows[indice].Enabled = false;
                        break;
                }
                carregar();
            }
        }

        protected void gv_lista_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            tb_o to = new tb_o();
            oss os = new oss();
            if (e.Row.RowType != DataControlRowType.DataRow)
                return;
            ((Button)e.Row.FindControl("btnApagar")).CommandArgument = e.Row.RowIndex.ToString();
           
            to = os.getNumOs(Convert.ToInt32(e.Row.Cells[0].Text));
            
                e.Row.Enabled = !(to.stts == 99);
            
        }

        //TODO:END



        protected void gv_lista_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            oss o = new oss();
            gv_lista.PageIndex = e.NewPageIndex;
            gv_lista.DataSource = o.listOss(Convert.ToInt16(Session["IDemp"]));
            gv_lista.DataBind();
            o = null;
        }

    }
}